package com.ruix.app;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.multidex.MultiDex;

import com.bumptech.glide.GlideBuilder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.decoder.ProgressiveJpegConfig;
import com.facebook.imagepipeline.image.QualityInfo;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.easeui.EaseUI;
import com.ruix.app.constant.ReleaseConstant;
import com.ruix.app.constant.RuiXConstant;
import com.ruix.app.manager.UserManager;
import com.ruix.app.network.RequestManager;
import com.ruix.app.service.MusicService;
import com.ruix.app.util.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;

import cn.jpush.android.api.JPushInterface;

//import cn.jpush.android.api.JPushInterface;


/**
 * Created by hadexs on 2017/4/10.
 */

public class RuiApplication extends Application {

    private static RuiApplication instance;
    private static Context mContext;

    public static String appVersionCode;// app版本号
    public static String appVersionName;// app版本名称

//    private HttpProxyCacheServer proxy;//缓存器

    private MusicService musicService;

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }

    public void init() {
        instance = this;
        mContext = getApplicationContext();
        initVersionData();// 初始化版本数据
        initRequst();
        initToastUtil();
        initImageLoader();
        initUmeng();  //
        initWXApi(); //初始化微信
        initEMClient(); //初始化环聊
        initJPush(); //
        UserManager.sharedInstance();
    }


    public static RuiApplication getInstance() {
        return instance;
    }

    // 初始化版本数据
    private void initVersionData() {
        try {
            appVersionCode = String.valueOf(mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode);
            appVersionName = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void initRequst() {
        RequestManager.init(mContext, appVersionCode, appVersionName);
    }

    // 初始化Toast提示框
    private void initToastUtil() {
        ToastUtil.initialize(mContext);
    }

    public static Context getAppContext() {
        return mContext;
    }

    // 初始化图片加载器
    private void initImageLoader() {
        ProgressiveJpegConfig config = new ProgressiveJpegConfig() {
            @Override
            public int getNextScanNumberToDecode(int i) {
                return 3;
            }

            @Override
            public QualityInfo getQualityInfo(int scanNumber) {
                return null;
            }

        };

        ImagePipelineConfig imagePipelineConfig = ImagePipelineConfig.newBuilder(this)
                .setProgressiveJpegConfig(config)
                .build();

        Fresco.initialize(this, imagePipelineConfig);// 初始化Fresco
    }


    private void initUmeng() {
        String umengkey = "5902a2b704e2058dd900234d";
        String umengChanel = "ruix";
        MobclickAgent.UMAnalyticsConfig config = new MobclickAgent.UMAnalyticsConfig(mContext.getApplicationContext(), umengkey, umengChanel);
        MobclickAgent.enableEncrypt(true);
        MobclickAgent.startWithConfigure(config);


        PlatformConfig.setWeixin(RuiXConstant.WX_APP_ID, RuiXConstant.WX_APP_AppSecret);
        UMShareConfig shareConfig = new UMShareConfig();
        shareConfig.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(mContext).setShareConfig(shareConfig);
    }

    private void initWXApi() {
        //

    }

    private void initJPush() {
        if (ReleaseConstant.ISDEBUG){
            JPushInterface.setDebugMode(true);
        }
        JPushInterface.init(this);
    }

    private void initEMClient() {
        // 不能多次初始化
        EMOptions options = new EMOptions();
        options.setAppKey("1175170412178384#ruix");
        options.setAutoLogin(true);
        options.setAutoAcceptGroupInvitation(true);
        options.setSortMessageByServerTime(true);
        options.setDeleteMessagesAsExitGroup(false);
        EaseUI.getInstance().init(mContext, options);

//        EMClient.getInstance().init(mContext,options);

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private void initGlide() {
        GlideBuilder builder = new GlideBuilder(mContext);
//        builder.setBitmapPool()
//        builder.

//        Glide.buildModelLoader();

    }



//    public static HttpProxyCacheServer getProxy(Context context) {
//        RuiApplication application =(RuiApplication) context;
//        return application.proxy == null ? (application.proxy = application.newProxy()) : application.proxy;
//    }
//
//    private HttpProxyCacheServer newProxy() {
//        return new HttpProxyCacheServer(this);
//    }

    public MusicService getMusicService() {
        return musicService;
    }

    public void setMusicService(MusicService musicService) {
        this.musicService = musicService;
    }
}
